/**
 * 文件名称:  TBcRoleContoller.java
 * 文件序号:  序号
 * 内容描述:  TBcRoleContoller控制层
 * 版权声明:  山东鸿蝶信息技术有限公司
 * 创建用户:  MissJin
 * 创建日期:  2017/1/10
 * 修改用户:  无
 * 修改日期:  无
 * 修改说明:  无
 * 文件版本:
 * 文件路径:
 */
package com.hongdee.rbac.role.controller;

import com.hongdee.base.entity.ResultEntity;
import com.hongdee.common.util.EntityUtils;
import com.hongdee.common.util.TokenUtil;
import com.hongdee.rbac.role.entity.TBcRole;
import com.hongdee.rbac.role.service.ITBcRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

//import com.sun.xml.internal.ws.api.message.HeaderList;

/**
 * Created by My on 2017/1/10.
 */
@RestController
@RequestMapping("/rbac/role")
public class TBcRoleController {
    @Autowired
    private ITBcRoleService itBcRoleService;

    /**
     * 方法名称: page
     * 方法描述: 分页查询
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestParam(name = "searchValue", defaultValue = "") String searchValue) throws Exception {
        Map map = itBcRoleService.page(start, limit, uid, searchValue);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 方法名称:info
     * 方法描述:通过 ID 查询实体信息
     * 返回内容:ResultEntity 封装类,msg\success\data属性有效
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/info")
    @ResponseBody
    public ResultEntity info(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", defaultValue = "") String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请先选择角色！");
        }
        ResultEntity res = new ResultEntity();
        Map map = itBcRoleService.info(id);
        res.setData(map);
        res.setSuccess(true);
        res.setMsg("查询详情成功");
        return res;
    }

    @RequestMapping("/infoRole")
    @ResponseBody
    public ResultEntity infoRole(@RequestParam(name = "uid", defaultValue = "") String uid,
                                 @RequestParam(name = "token", defaultValue = "") String token,
                                 @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请先选择角色！");
        }
        ResultEntity res = new ResultEntity();
        boolean flag = itBcRoleService.checkCompanyInfo(uid, id);
        if (flag) {
            Map map = itBcRoleService.info(id);
            res.setData(map);
            res.setSuccess(true);
            res.setMsg("查询详情成功");
        } else {
            Map<String, String> map = TokenUtil.getCompany(uid);
            res.setSuccess(false);
            res.setMsg("该角色不隶属于" + map.get("oName") + "，暂无权限操作！！");
        }
        return res;
    }


    /**
     * 方法名称: res
     * 方法描述: 查询角色资源
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/res")
    @ResponseBody
    public ResultEntity res(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择角色！");
        }
        ResultEntity res = new ResultEntity();
        List datalist = this.itBcRoleService.res(id);
        res.setSuccess(true);
        res.setMsg("查询成功！");
        res.setDataList(datalist);
        return res;
    }

    /**
     * 方法名称: stop
     * 方法描述: 根据主键停用角色
     * 返回内容:ResultEntity 有效属性msg/success
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/stop")
    @ResponseBody
    public ResultEntity stop(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "id", required = false) String id) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请先选择角色！");
        }
        //根据主键停用
        String status = "0";
        this.itBcRoleService.startAndStopRole(uid, id, status);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("停用成功");
        return res;
    }

    /**
     * 方法名称: start
     * 方法描述: 根据主键启用角色
     * 返回内容:ResultEntity 有效属性msg/success
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/start")
    @ResponseBody
    public ResultEntity start(@RequestParam(name = "uid", defaultValue = "") String uid,
                              @RequestParam(name = "token", defaultValue = "") String token,
                              @RequestParam(name = "id", required = false) String id) throws Exception {

        if (StringUtils.isEmpty(id)) {
            throw new Exception("请先选择角色！");
        } //根据主键停用
        String status = "1";
        this.itBcRoleService.startAndStopRole(uid, id, status);
        ResultEntity res = new ResultEntity();
        res.setSuccess(true);
        res.setMsg("启用成功");
        return res;
    }

    /**
     * 方法名称:save
     * 方法描述:保存入库单数据
     * 返回内容:
     * 异常说明:
     * 创建用户:
     * 创建时间:
     * 修改用户:
     * 修改时间:
     * 修改内容:
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody TBcRole data) throws Exception {
        //判断必填项
        EntityUtils.valid(data);
        ResultEntity res = new ResultEntity();
        boolean flag = false;
        if ("".equals(data.getId()) || null == data.getId()) {
            flag = true;
        } else {
            flag = itBcRoleService.checkCompanyInfo(uid, data.getId());
        }
        if (flag) {
            TBcRole tm = this.itBcRoleService.save(data, uid);
            res.setMsg("保存成功！");
            res.setSuccess(true);
            res.setData(tm);
        } else {
            Map<String, String> map = TokenUtil.getCompany(uid);
            res.setSuccess(false);
            res.setMsg("该角色不隶属于" + map.get("oName") + "，暂无权限操作！！");
        }
        return res;
    }

    /**
     * @param uid    当前登录用户 ID
     * @param token  当前登录用户 token
     * @param id     角色主键
     * @param resIds
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveRes")
    @ResponseBody
    public ResultEntity saveRes(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id,
                                @RequestBody String[] resIds) throws Exception {
        if (StringUtils.isEmpty(id)) {
            throw new Exception("请选择角色");
        }
        this.itBcRoleService.saveRes(id, resIds);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;

    }

    /**
     * @param uid   当前登录用户 ID
     * @param token 当前登录用户 token
     * @param type  角色类型，默认为资源角色
     * @return
     * @throws Exception 基类异常
     * @desc 用户分配资源角色时，所需的角色树列表
     * @author zhangkai
     * @date 2017/2/11
     * @time 09:32
     * @update_user
     * @update_time
     * @update_desc:
     */

    @RequestMapping("/tree")
    @ResponseBody
    public ResultEntity tree(@RequestParam(name = "uid", defaultValue = "") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestParam(name = "type", defaultValue = "0") String type) throws Exception {
        List treeList = this.itBcRoleService.tree(uid, type);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询树列表成功");
        return res;
    }

    /***
     *
     * @param uid 登录用户 id
     * @param token 登录用户 Token
     * @param rId 角色 ID
     * @return ResultEntity
     * @throws Exception
     */
    @RequestMapping("/resTree")
    @ResponseBody
    public ResultEntity resTree(@RequestParam(name = "uid", defaultValue = "") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "rId", defaultValue = "") String rId) throws Exception {
        if (StringUtils.isEmpty(rId)) {
            throw new Exception("请选择角色！");
        }
        List treeList = this.itBcRoleService.checkedResTree(rId);
        ResultEntity res = new ResultEntity();
        res.setDataList(treeList);
        res.setSuccess(true);
        res.setMsg("查询角色对应的资源树成功");
        return res;
    }

    /**
     * @param
     * @return Map
     * @throws Exception
     * @desc 角色转换
     * @author 徐钦勇
     */
    @RequestMapping("/convert")
    @ResponseBody
    public Map convert() throws Exception {
        return this.itBcRoleService.convert();
    }

    /**
     * 首页切换角色，获取资源角色
     * @param uid
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping("/roleDateList")
    @ResponseBody
    public ResultEntity roleDateList(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token) throws Exception {
        ResultEntity res=new ResultEntity();
        List<Map<String, Object>> list=itBcRoleService.getRoleDateByUid(uid);
        res.setData(list);
        res.setSuccess(true);
        return res;
    }

    /*
* @param
* @return Map
* @throws Exception
* @desc 下拉列表框
* @author 于扬
* */
    @RequestMapping("/combo")
    @ResponseBody
    public List<Map> combo(@RequestParam(name = "uid", defaultValue = "") String uid,
                           @RequestParam(name = "token", defaultValue = "") String token
    ) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        System.out.println(oid);
        return this.itBcRoleService.combo(oid);
    }

    @RequestMapping("/Scmconvert")
    @ResponseBody
    public Map scmconvert(@RequestParam(name = "uid", defaultValue = "") String uid
    ) throws Exception {
        String oid = TokenUtil.getCompanyId(uid);
        return this.itBcRoleService.scmconvert(oid);
    }
}
